var documentModule = angular.module('pcsDocument', ["angularFileUpload"]);


documentModule.controller('documentCtrl', [
			'$rootScope',
			'$scope',
			'$http',
			'FileUploader',function($rootScope, $scope, $http, FileUploader) {
				
				$rootScope.userInfo = {"token":""};
				$rootScope.userInfo.token = access_token;

				var restUrl = {
					"upload": "/rest/documents/upload?access_token=",
					"list": "/rest/documents?access_token=",
					"update": "/rest/documents/",
					"delete": "/rest/documents/",
					"share": "/rest/documents/",
					"myshareDoc": "/rest/documents/share/my_share_documents",
					"shareWithDoc": "/rest/documents/share/share_me_documents"
				};
				$scope.fileInfo = {
					"id": "",
					"path": "",
					"ondup": ""
				};
				$scope.fileDesc = {
					"docState": "Uploaded",
				};


				$scope.files = null;

				$scope.weblinkPermission = "0";
				$scope.weblink = {
					"permission": parseInt($scope.weblinkPermission),
					"shareMessage": ""
				};
				$scope.weblinkRet = null; //only public share result

				$scope.sharePermission = "0";
				$scope.sharees = {
					"permission": parseInt($scope.sharePermission),
					"email": ""
				}
				$scope.share = {
					"shareMessage": "",
					"sharees": ""
				};
				$scope.shareRet = null; //only private share result
				$scope.allShareRet = null; //private + public share result

				$scope.shareWithMe = null;
				$scope.myShare = null;

				$scope.updatePermission = "0";
				$scope.shareUpdateAttr = {
					"id": "",
					"permission": null,
					"shareMessage": ""
				}
				
				
				var uploader = $scope.uploader = new FileUploader({
					scope: $scope,
					method: 'PUT',
					crossDomain: true,
					removeAfterUpload: true,
					xhrFields: {
						withCredentials: true
					}
				});

				uploader.onAfterAddingFile = function(item) {
					$scope.fileInfo.path = item.file.name;
				}

				$scope.deleteDoc = function(id) {
					$http.delete(restUrl.delete + id + "?access_token=" + $rootScope.userInfo.token)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.list();
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								}
							},
							function(errResponse) {
								console
									.error('Error while fetching notes');
							});
				}

				$scope.list = function() {
					var expand = [];
						expand.push("Library");
					var url = restUrl.list + $rootScope.userInfo.token;
						url +="&expand=";
						url += expand;
					$http.get(url)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.files = [];
								    
									for(var i = 0 ; i < ret.data.length; i++){
										$scope.files.push(ret.data[i]);
										$scope.files[i].library = {id:"",name:""};
										if(ret.expand[i].Library != null ){
											var data = ret.expand[i].Library;
											$scope.files[i].library.id  = data.id;
											$scope.files[i].library.name  = data.name;
										}else{
											$scope.files[i].library.id = "";
											$scope.files[i].library.name = "";
										}
									}
								
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								} },
							function(errResponse) {
								console.error('Error while fetching notes'); });
				}
				$scope.list();
				
				$scope.info = function(id) {
					$http.get(restUrl.delete + id + "?access_token=" + $rootScope.userInfo.token)
					.then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.fileInfo = ret.data;
							} else{
								noty({text:ret.message, type: 'warning',layout:'center'});
								console.error('Request failed: ' + ret);
							}
						},
						function(errResponse) {
							console
								.error('Error while fetching notes');
						});
				};
				
				$scope.update = function() {
					$http
						.put(
							restUrl.update + $scope.fileInfo.id + "?access_token=" + $rootScope.userInfo.token,
							$scope.fileDesc)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.list();
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								} },
							function(errResponse) {
								console.error('Error while fetching notes');
							});
				};
				
				$scope.setDocId = function(id) {
					$scope.fileInfo.id = id;
				};
				
				$scope.setUpdateDocShare = function(id, shareMessage, permissions) {
					
					$scope.shareUpdateAttr.id = id;
//					alert($scope.shareUpdateAttr.id);
					$scope.permission = parseInt(permissions);
					$scope.shareUpdateAttr.shareMessage = shareMessage;
				};

				$scope.upload = function() {
					if($scope.fileInfo.ondup == ""){
						noty({text:"请选择是否同名!", type: 'warning',layout:'center'});
						return false;
					}
					$http
						.post(
							restUrl.upload + $rootScope.userInfo.token,
							$scope.fileInfo)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									console.log(ret.data)
									$scope.fileInfo.id = ret.data.id;
									$rootScope.fileInfo = {};
									$rootScope.fileInfo.id = ret.data.id;
									uploader.queue[0].url = uploader.url = ret.data.s3SignedUrl;
									uploader.queue[0].upload();
									$scope.update();
									
									servicemain.addmodalControl(1);
									noty({text:"upload success!", type: 'warning',layout:'center'});
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								}
									
							},
							function(errResponse) {
								console.error('Error while fetching notes');
							});
				};

				$scope.createWebLink = function() {
					$scope.weblink.permission = parseInt($scope.weblinkPermission);
					$http.post(
						restUrl.share + $scope.fileInfo.id + "/weblink" + "?access_token=" + $rootScope.userInfo.token,
						$scope.weblink).then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.weblinkRet = ret.data;
								noty({text:ret.message, type: 'warning',layout:'center'});
							} else{
								noty({text:ret.message, type: 'warning',layout:'center'});
								console.error('Request failed: ' + ret);
							}
							
						},
						function(errResponse) {
							console.error('Error while fetching notes');
						});

				};


				$scope.createShare = function() {
					
					$scope.sharees.permission = parseInt($scope.sharePermission);
					var shareeArray = new Array();
					var emailArr = $scope.sharees.email.split(",");
					
					for(var i = 0 ; i < emailArr.length; i++){
						var obj = {};
						obj.email = emailArr[i];
						obj.permission = $scope.sharees.permission;
						shareeArray.push(obj);
					}
					
					$scope.share.sharees = shareeArray;
					$http.post(
						restUrl.share + $scope.fileInfo.id + "/share" + "?access_token=" + $rootScope.userInfo.token,
						$scope.share).then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.shareRet = ret.data;
								servicemain.addmodalControl(1);
								noty({text:"upload success!", type: 'warning',layout:'center'});
							} else{
								noty({text:ret.message, type: 'warning',layout:'center'});
								console.error('Request failed: ' + ret);
							}
								
						},
						function(errResponse) {
							console.error('Error while fetching notes');
						});

				};

				$scope.listDocumentShare = function(id) {
					if(id != null && id != "undefine" && id != ""){
						$scope.fileInfo.id = id;
					}

					if($scope.fileInfo.id == null || $scope.fileInfo.id == "undefine" || $scope.fileInfo.id == ""){
						noty({text:"please change document!", type: 'warning',layout:'center'});
					}
					
					$http.get(
						restUrl.share + $scope.fileInfo.id + "/share" + "?access_token=" + $rootScope.userInfo.token).then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.allShareRet = ret.data;
//								alert($scope.allShareRet.length);
							} else{
								noty({text:ret.message, type: 'warning',layout:'center'});
								console.error('Request failed: ' + ret);
							}
						},
						function(errResponse) {
							console.error('Error while fetching notes');
						});

				};

				$scope.deleteDocShare = function(id) {
					$http.delete(
							restUrl.share + $scope.fileInfo.id + "/share/" + id + "?access_token=" + $rootScope.userInfo.token)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.listDocumentShare();
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								} },
							function(errResponse) {
								console.error('Error while fetching notes'); });
				};

				$scope.delAllShare = function() {
					$http.delete(
							restUrl.share + $scope.fileInfo.id + "/share/?access_token=" + $rootScope.userInfo.token)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.shareRet = null;
									$scope.weblinkRet = null;
									$scope.listDocumentShare();
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								}
							},
							function(errResponse) {
								console
									.error('Error while fetching notes');
							});
				};


				$scope.updateDocShare = function(id) {
					$scope.shareUpdateAttr.permission = parseInt($scope.updatePermission);
					$http.put(
							restUrl.share + $scope.fileInfo.id + "/share/" + id + "?access_token=" + $rootScope.userInfo.token, $scope.shareUpdateAttr)
						.then(
							function(response) {
								ret = response.data;
								if (ret.ret == 0) {
									$scope.listDocumentShare();
									noty({text:"update success!", type: 'warning',layout:'center'});
								} else{
									noty({text:ret.message, type: 'warning',layout:'center'});
									console.error('Request failed: ' + ret);
								}
							},
							function(errResponse) {
								console
									.error('Error while fetching notes');
							});
				};
				
				
				
				
				$scope.myshareDoc = function() {
					$http.get(
						restUrl.myshareDoc + "?access_token=" + $rootScope.userInfo.token).then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.myShare = ret.data;
							} else
								console.error('Request failed: ' + ret);
						},
						function(errResponse) {
							console.error('Error while fetching notes');
						});
				};
//				$scope.myshareDoc();

				$scope.shareWithDoc = function() {
					$http.get(
						restUrl.shareWithDoc + "?access_token=" + $rootScope.userInfo.token).then(
						function(response) {
							ret = response.data;
							if (ret.ret == 0) {
								$scope.shareWithMe = ret.data;
							} else
								console.error('Request failed: ' + ret);
						},
						function(errResponse) {
							console
								.error('Error while fetching notes');
						});
				};
//				$scope.shareWithDoc();

			}
		]);